package com.yonyou.pmclouds.print.util;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;

public class ParamMapUtil {
    public static Map<String,String> getParameterMap(Map paramMap)
            throws UnsupportedEncodingException {
        Map returnMap = new HashMap();
        Iterator entries = paramMap.entrySet().iterator();

        String name = "";
        String value = "";
        while (entries.hasNext()) {
            Entry entry = (Entry) entries.next();
            name = (String) entry.getKey();
            Object valueObj = entry.getValue();
            if (valueObj == null) {
                value = "";
            } else if ((valueObj instanceof String[])) {
                String[] values = (String[]) valueObj;
                for (int i = 0; i < values.length; i++) {
                    value = values[i] + ",";
                }
                value = value.substring(0, value.length() - 1);
            } else {
                value = valueObj.toString();
            }
            returnMap.put(name.replaceAll("%(?![0-9a-fA-F]{2})", "%25"), URLDecoder.decode(value.replaceAll("%(?![0-9a-fA-F]{2})", "%25"), "UTF-8"));
        }
        returnMap.remove("print_token");
        return returnMap;
    }
}
